﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace rInvoice
{
    public partial class Bank : UserControl
    {
        ServerObject mServerObject = null;
        string messageText = string.Empty;

        public Bank()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }       

        private void Bank_Load(object sender, EventArgs e)
        {
            FillGridView();
        }

        private void FillGridView()
        {
            DataTable bankTable = mServerObject.SelectBankList(false);
            dataGridView1.DataSource = bankTable;
        }

        private void newButton_Click(object sender, EventArgs e)
        {
            BankInputPanel AddForm = new BankInputPanel();
            AddForm.CreateNew = true;

            DialogResult resultDialog = AddForm.ShowDialog();
            if (resultDialog == DialogResult.OK)
            {
                messageText = "Data was saved";
            }
            else
            {
                messageText = "Sorry, data was not saved";
            }
            MessageBox.Show(messageText);
            FillGridView();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                int mBankID = 0;

                string bnkID = dataGridView1.Rows[e.RowIndex].Cells["BankID"].Value.ToString();

                int.TryParse(bnkID, out mBankID);
                DataRow rowData = mServerObject.SelectBankByID(mBankID);

                BankInputPanel bankInputPanel = new BankInputPanel();

                bankInputPanel.BankID = mBankID;
                bankInputPanel.BankCod = rowData["BankCod"].ToString();
                bankInputPanel.BankAccount = rowData["BankAccount"].ToString();
                bankInputPanel.BankName = rowData["BankName"].ToString();
                
                try
                {
                    DialogResult resultDialog = bankInputPanel.ShowDialog();
                    if (resultDialog == DialogResult.OK)
                    {
                        messageText = "Data was saved";
                    }
                    else
                    {
                        messageText = "Sorry, data was not saved";
                    }
                    MessageBox.Show(messageText);
                }
                finally
                {
                    FillGridView();
                }
            }
        }

        private void selectButton_Click(object sender, EventArgs e)
        {
            FillGridView();
        }
    }
}
